package proto

import (
	"context"
	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/utils/log"
	"google.golang.org/grpc"
)

type Version interface {
	GetVersion() string
}

const currentVersion = "1.0"
const RobotReqVersion = "1.0"

func CheckVersion(req Version) bool {
	return req.GetVersion() == currentVersion
}

func CheckVersionGrpcInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
	versionReq, ok := req.(Version)
	if ok {
		if !CheckVersion(versionReq) {
			log.ErrorfWithContext(ctx, "request version not match,current version:%s ,get version:%s", currentVersion, versionReq.GetVersion())
			return nil, codes.ErrWrongRequestVersion
		}
	}
	return handler(ctx, req)
}
